package servlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import model.User;
import service.UserService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
//管理员用户获取列表
@WebServlet(name = "UserAdminList", value = "/UserAdminList")

public class UserAdminList extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        //告诉浏览器，服务器发送的消息体的数据的编码
        response.setHeader("content-type", "text/html;charset=utf-8");
        PrintWriter pw =response.getWriter();
        if (userService.getUserList() != null) {
            List<User> userList = new ArrayList<>();
            userList = userService.getUserList();
//            System.out.println(userList);//测试
            this.getServletContext().setAttribute("userList", userList);
            pw.print("<script>alert('获取数据成功~~~~！'); window.location='userlistAdmin.jsp' </script>");
        } else {
            pw.print("<script>alert('获取数据失败~~~~！'); window.location='userlistAdmin.jsp' </script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}